//
//  SearchViewController.m
//  Copy_AiKapPu_Personal_OC
//
//  Created by taojianz on 2017/7/11.
//  Copyright © 2017年 taojianz. All rights reserved.
//

#import "SearchViewController.h"
#import "AKPSearchTableViewCell.h"
#import "MJRefresh.h"
#import <nbappdev/nbappdev.h>
#import "AKPConditionalSearchTableViewController.h"

@interface SearchViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
//@property (strong,nonatomic) UIRefreshControl *refreshController;
@property (strong,nonatomic) NSMutableArray *userArray;
@property (weak, nonatomic) IBOutlet UIButton *smartSorting;
@property (weak, nonatomic) IBOutlet UIButton *degreeSorting;
@property (weak, nonatomic) IBOutlet UIButton *distanceSorting;

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    CGRect rect = CGRectMake(0, 0, 0, CGFLOAT_MIN);
    _tableView.tableHeaderView.frame = rect;
    [self setupRefreshController];

    
    NSArray *array = @[@{@"avatar":@"jiehun-hui",@"name":@"name",@"score":@"98",@"distance":@"3.3",@"age":@"18",@"height":@"127",
                    @"education":@"小学生",@"company":@"宁波优跨信息科技有限公司"},
                @{@"avatar":@"jiehun-hui",@"name":@"name",@"score":@"98",@"distance":@"3.3",@"age":@"18",@"height":@"127",
                    @"education":@"小学生",@"company":@"宁波优跨信息科技有限公司"},
                @{@"avatar":@"jiehun-hui",@"name":@"name",@"score":@"98",@"distance":@"3.3",@"age":@"18",@"height":@"127",
                    @"education":@"小学生",@"company":@"宁波优跨信息科技有限公司"},
                @{@"avatar":@"jiehun-hui",@"name":@"name",@"score":@"98",@"distance":@"3.3",@"age":@"18",@"height":@"127",
                    @"education":@"小学生",@"company":@"宁波优跨信息科技有限公司"},
                @{@"avatar":@"jiehun-hui",@"name":@"name",@"score":@"98",@"distance":@"3.3",@"age":@"18",@"height":@"127",
                    @"education":@"小学生",@"company":@"宁波优跨信息科技有限公司"}];
    
    _userArray = [[NSMutableArray alloc] initWithArray:array];
    [self.navigationItem.rightBarButtonItems[0] setAction:@selector(setupRightItemAction)];
    [self.navigationItem.rightBarButtonItems[0] setTarget:self];
    
    [self.smartSorting setSelected:true];

}
// 设置rightButtonItem的点击事件
-(void)setupRightItemAction{
    AKPConditionalSearchTableViewController *vc = [Helper getViewController:@"AKPConditionalSearchTableViewController" withStoryboard:@"AKPSearch"];
    [self.navigationController pushViewController:vc animated:true];
    
    
}

// FIXME: 进行真正的排序
- (IBAction)smartSortBtnTapped:(UIButton *)sender {
    [sender setSelected:true];
    [self.degreeSorting setSelected:false];
    [self.distanceSorting setSelected:false];
    [self.tableView.mj_header beginRefreshing];
}
- (IBAction)egreeSortBtnTapped:(UIButton *)sender {
    [sender setSelected:true];
    [self.smartSorting setSelected:false];
    [self.distanceSorting setSelected:false];
    [self.tableView.mj_header beginRefreshing];
}
- (IBAction)distanceSortBtnTapped:(UIButton *)sender {
    [sender setSelected:true];
    [self.smartSorting setSelected:false];
    [self.degreeSorting setSelected:false];
    [self.tableView.mj_header beginRefreshing];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _userArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    AKPSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchHomeTableCell" forIndexPath:indexPath];
    
    NSDictionary *dict = [_userArray objectAtIndex:indexPath.section];
    [cell setContentByDict:dict];
    
    return cell;
}

// 取消点击cell 之后的select 效果
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:true];
}

// 代码去除TableView 顶部的间隙
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
// 设置refreshController
-(void)setupRefreshController{
    // 设置 下拉刷新控件
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadSearchViewData)];
    
    [self.tableView.mj_header beginRefreshing];
    
    // 设置上拉刷新控件
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadSearchViewDataByDropDown)];
    
}
// 模拟下拉刷新数据
-(void)loadSearchViewData{
    NSInteger count = [self.tableView numberOfSections];
    
    NSString *str_C = [[[NSString alloc] initWithFormat:@"%zd", count+1] stringByAppendingString:@"号小学生"];
    
    NSDictionary *newUser = @{@"avatar":@"jiehun-hui",@"name":str_C,@"score":@"98",@"distance":@"3.3",@"age":@"18",@"height":@"127",
                            @"education":@"小学生",@"company":@"宁波优跨信息科技有限公司"};
    [_userArray insertObject:newUser atIndex:0];
    
    [self.tableView reloadData];
    
    [self performSelector:@selector(endRefreshing) withObject:nil afterDelay:2];
    
    
    NSLog(@"现有数据条数:%zd",[self.tableView numberOfSections]);
}
// 模拟上拉刷新数据
-(void)loadSearchViewDataByDropDown{
    NSInteger count = [self.tableView numberOfSections];
    
    NSString *str_C = [[[NSString alloc] initWithFormat:@"%zd", count+1] stringByAppendingString:@"号小学生"];
    
    NSDictionary *newUser = @{@"avatar":@"jiehun-hui",@"name":str_C,@"score":@"98",@"distance":@"3.3",@"age":@"18",@"height":@"127",
                              @"education":@"小学生",@"company":@"宁波优跨信息科技有限公司"};
    [_userArray addObject:newUser];
    [self.tableView reloadData];
    
    [self performSelector:@selector(endRefreshing) withObject:nil afterDelay:2];
    
    
    NSLog(@"现有数据条数:%zd",[self.tableView numberOfSections]);
}
-(void)endRefreshing{
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
}


@end


